import { useQuery } from '@tanstack/react-query';
import { Form } from 'antd';
import { useState, useEffect } from 'react';

import commonService from '@/api/services/commonService';

type ModalProps = {
  formValue: any;
  title: string;
  show: boolean;
  type: string;
  onOk: Function;
  onCancel: VoidFunction;
};

export function useCommon(searchParams: any, uri: string, modalParams: any, cateUri?: string): any {
  const [searchForm] = Form.useForm();
  const [cateList, setCateList] = useState([]);
  const requestUri: any = uri || cateUri;
  const [query, setQuery] = useState<any>({
    ...searchParams,
    pageSize: 10,
    pageNo: 1,
  });
  useEffect(() => {
    if (cateUri && uri) {
      commonService.getCateListApi(cateUri, { pageNo: 1, pageSize: 500 }).then((res) => {
        setCateList(res);
      });
    }
  }, [cateUri, uri]);

  const [ModalPros, setModalProps] = useState<ModalProps>({
    formValue: {
      ...modalParams,
    },
    type: 'add',
    title: '新增',
    show: false,
    onOk: async (values: any, type?: any) => {
      const params = { ...values };
      if (!params.id) {
        delete params.id;
        await commonService.addApi(requestUri, params);
      } else if (type === 'audit') {
        await commonService.auditApi(requestUri, params);
      } else {
        await commonService.editApi(requestUri, params);
      }
      setModalProps((prev) => ({ ...prev, show: false }));
      refetch();
    },
    onCancel: () => {
      setModalProps((prev) => ({ ...prev, show: false }));
    },
  });

  const onCreate = (formValue: any) => {
    setModalProps((prev) => ({
      ...prev,
      show: true,
      type: 'add',
      title: '新增',
      formValue: {
        ...prev.formValue,
        ...formValue,
      },
    }));
  };

  const onEdit = (formValue: any) => {
    commonService
      .getApi(requestUri === 'user' ? 'user-view' : requestUri, { domainId: formValue.id })
      .then((res) => {
        setModalProps((prev) => ({
          ...prev,
          show: true,
          type: 'edit',
          title: '编辑',
          formValue: {
            ...res,
          },
        }));
      });
  };

  const onRead = (formValue: any) => {
    commonService.getApi(requestUri, { domainId: formValue.id }).then((res) => {
      setModalProps((prev) => ({
        ...prev,
        show: true,
        type: 'read',
        title: '查看',
        formValue: {
          ...res,
        },
      }));
    });
  };

  const handleChange = (e: any) => {
    setQuery((prev: any) => ({
      ...prev,
      pageNo: e.current,
    }));
  };

  const { data, error, isLoading, refetch } = useQuery({
    queryKey: [requestUri, query],
    queryFn: async () => {
      const params: any = {};
      Object.keys(query).forEach((key) => {
        if (query[key] || query[key] === 0) {
          params[key] = query[key];
        }
      });
      let requestApi = commonService.getListApi;
      if (cateUri && !uri) {
        requestApi = commonService.getCateListApi;
      }
      if (params.groupId && params.groupId.length) {
        return requestApi(requestUri, { ...params, groupId: params.groupId[1] });
      }
      if (params.catalogueId && params.catalogueId.length) {
        return requestApi(requestUri, { ...params, catalogueId: params.catalogueId[1] });
      }
      return requestApi(requestUri === 'user' ? 'user-view' : requestUri, { ...params });
    },
  });

  const onSearch = (values: any) => {
    setQuery((prev: any) => ({
      ...prev,
      ...values,
      pageNo: 1,
    }));
  };

  const onSwitch = (id: any) => {
    commonService.setStatusApi(requestUri, { domainId: id }).then(() => {
      refetch();
    });
  };

  const onSwitchEdit = (id: any) => {
    commonService.setEditApi(requestUri, { domainId: id }).then(() => {
      refetch();
    });
  };

  const onSwitchTop = (id: any) => {
    commonService.setTopApi(requestUri, { domainId: id }).then(() => {
      refetch();
    });
  };

  const onSearchFormReset = () => {
    searchForm.resetFields();
  };

  const onUploaded = (key: any, res: any) => {
    console.log(key, res, 2222);
  };

  const doDel = (domainId: any) => {
    commonService.delApi(requestUri, { domainId }).then(() => {
      refetch();
    });
  };

  return {
    query,
    data,
    error,
    isLoading,
    cateList,
    ModalPros,
    searchForm,
    refetch,
    onCreate,
    onSearch,
    onSearchFormReset,
    handleChange,
    onSwitch,
    onSwitchEdit,
    onSwitchTop,
    onEdit,
    onRead,
    doDel,
  };
}
